﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Threading;
using BrainTechLLC;
using ImageAppViewModels;
using System.Windows.Threading;
using ImageAppDataModels;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.Generic;

namespace WpfImageDuplicates
{
    public partial class ThumbnailView : UserControl
    {
        protected FastSmartWeakEvent<EventHandler<ObjectEventArgs<FileImageViewModel>>> _wantView;
        public event EventHandler<ObjectEventArgsNonRef<bool>> ItemSelectedByUser;
        public event EventHandler<ObjectEventArgsNonRef<bool>> ItemDeselectedByUser;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResizeDelete;
        public event EventHandler<ObjectEventArgsNonRef<RotationParams, bool>> RequestRotate;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResize;
        protected FastSmartWeakEvent<EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>> _wantSetComparison;

        public event EventHandler<ObjectEventArgs<FileImageViewModel>> WantView
        {
            add
            {
                if (_wantView == null)
                {
                    Interlocked.CompareExchange<FastSmartWeakEvent<EventHandler<ObjectEventArgs<FileImageViewModel>>>>(ref _wantView, new FastSmartWeakEvent<EventHandler<ObjectEventArgs<FileImageViewModel>>>(), null);
                }
                _wantView.Add(value);
            }
            remove
            {
                if (_wantView == null)
                    return;

                _wantView.Remove(value);
            }
        }        

        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> WantSetComparison
        {
            add
            {
                if (_wantSetComparison == null)
                {
                    Interlocked.CompareExchange<FastSmartWeakEvent<EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>>>(ref _wantSetComparison, new FastSmartWeakEvent<EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>>(), null);
                }
                _wantSetComparison.Add(value);
            }
            remove
            {
                if (_wantSetComparison == null)
                    return;

                _wantSetComparison.Remove(value);
            }
        }


        public bool MeetsFilterCriteria(string filter, int minSize, int maxSize, List<GroupViewModel> selectedGroups)
        {
            if (_vm == null)
                return false;

            return _vm.MeetsFilterCriteria(filter, minSize, maxSize, selectedGroups);
        }

        internal void OnThumbBecomesVisible()
        {            
            _vm.ThumbVisibleOnScreen = true;
            _parentCollectionView.ForceRefresh = true;

            if (!BindingOperations.IsDataBound(imgMain, Image.SourceProperty))
            {
                Binding b = new Binding("ThumbImageSourceCached") { Mode = BindingMode.OneWay };                
                imgMain.SetBinding(Image.SourceProperty, b);
            }
            if (_vm != null)
            {
                _vm.SetThumbEvent -= _vm_SetThumbEvent;
                _vm.SetThumbEvent += new EventHandler<ObjectEventArgs<FileImage>>(_vm_SetThumbEvent);
                _vm.Dirty = true;
            }
        }

        internal void OnThumbBecomesInvisible()
        {
            _vm.ThumbVisibleOnScreen = false;
            _vm.SetThumbEvent -= _vm_SetThumbEvent;
            BindingOperations.ClearBinding(imgMain, Image.SourceProperty);
            imgMain.Source = null;                       
        }
    }
}
